import java.math.*;
import java.awt.*;
import java.awt.event.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import javax.swing.JFrame;

import com.jogamp.common.nio.Buffers;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;


public class Cylinder extends Shape {
	private int height;

	public Cylinder(float radius, int height){
		centerX = 0;
		centerY = 0;
		centerZ = 0;
		scale = radius;
		this.height = height;
		color = 0;
	}
	
	public void draw(GL2 gl){
		
		vert = new float[150];
		normal = new float[150];
		int vIndi = 0;
	
		vert[vIndi] = 0;
		vIndi++;
		vert[vIndi] = height/2;
		vIndi++;
		vert[vIndi] = 0;
		vIndi++;
		
		//Top circle
		for(int i=0; i<24; i++) {
			float theta = (i)*(float)Math.PI/12;
			vert[vIndi] = scale*(float)Math.cos(theta);
			normal[vIndi] = (float)(Math.cos(theta)*Math.cos(theta));
			vIndi++;
			vert[vIndi] = height/2;
			normal[vIndi] = height/2;
			vIndi++;
			vert[vIndi] = scale*(float)Math.sin(theta);
			normal[vIndi] = (float)(Math.sin(theta)*Math.sin(theta));
			vIndi++;
		}
		
		vert[vIndi] = 0;
		vIndi++;
		vert[vIndi] = -height/2;
		vIndi++;
		vert[vIndi] = 0;
		vIndi++;
		//Bottom Circle
		for(int i=0; i<24; i++) {
			float theta = (i)*(float)Math.PI/12;
			vert[vIndi] = scale*(float)Math.cos(theta);
			normal[vIndi] = scale*(float)Math.cos(theta);
			vIndi++;
			vert[vIndi] = -height/2;
			normal[vIndi] = height/2;
			vIndi++;
			vert[vIndi] = scale*(float)Math.sin(theta);
			normal[vIndi] = scale*(float)Math.sin(theta);
			vIndi++;
		} 
		
		indi = new int[48*6];
		int in = 0;
		for(int i = 0; i < 24; i++){
			if((i+2) == 25)
				indi[in] = 1;
			else
				indi[in] = (i+2);
			in++;
			indi[in] = (i+1);
			in++;
			indi[in] = 0;
			in++;
		}
		
		for(int i = 25; i < 49; i++){
			indi[in] = 25;
			in++;
			indi[in] = (i+1);
			in++;
			if((i+2) == 50)
				indi[in] = 26;
			else
				indi[in] = (i+2);
			in++;
		}
		
		for(int i = 0; i < 24; i++){
			indi[in] = i+1;
			in++;
			if(i==23)
				indi[in] = 1;
			else
				indi[in] = i+2;
			in++;
			indi[in] = i+26;
			in++;
		}
		for(int i = 0; i < 24; i++){
			if(i == 23)
				indi[in] = 1;
			else
				indi[in] = i+2;
			in++;
			if(i==23)
				indi[in] = 26;
			else
				indi[in] = i+27;
			in++;
			indi[in] = i+26;
			in++;
		}
		
		vert = performTransform(vert, false);
		
		vBuf = Buffers.newDirectFloatBuffer(vert.length);
		vBuf.put(vert);
		vBuf.rewind();
		nBuf = Buffers.newDirectFloatBuffer(normal.length);
		nBuf.put(normal);
		nBuf.rewind();
		iBuf = Buffers.newDirectIntBuffer(indi.length);
		iBuf.put(indi);
		iBuf.rewind();
		
		
		gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glPushMatrix();

		gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
		
		gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vBuf);
		gl.glNormalPointer(GL2.GL_FLOAT, 0, nBuf);
		
		gl.glColor3f(1.0f, color, 0.0f); // Red
		gl.glDrawElements(GL2.GL_TRIANGLES, indi.length, GL2.GL_UNSIGNED_INT, iBuf);
		gl.glPopMatrix();
		gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);

	}
}
